	
/**
 * 页面初始化
 */
$(document).ready(function(){
	$.ajax({
		type : "POST",
		dataType : "json",
		url : '/iwash/goods/goods/listGoods.do',
		success : function(result) {
			console.info(result);
			$.each(result,function(i,g){
				var li='<li data='+g.id+'><div>商品名称:'+g.name+'<br>商品价格:¥ '+g.price+'<br><a href="javascript:void(0)" onclick="addGnum(this,-1)" id="decrement">-</a><input type="text" id="quantity" value="1"/><a href="javascript:void(0)" onclick="addGnum(this,1)" id="increment">+</a><a href="javascript:void(0)" onclick="addToCart(this)" id="addToCart-'+g.id+'">加入购物车</a></div></li>'
				$('.u-list').append(li);
			});
		}
	});
	
});

function addToCart(obj){
	var goodsId=$(obj).attr('id').split('-')[1];
	var quantity = parseInt($(obj).parent().find(":text").val());
	$.ajax({
		type : "POST",
		dataType : "json",
		url : '/iwash/cart/shoppingCart/changeGnumInCart.do',
		data : {"goodsId":goodsId,"quantity":quantity,"flag":1},
		success : function(result) {
			console.info(result);
		}
	});
}

function addGnum(obj,flag){
	var num = parseInt($(obj).parent().find(":text").val());
	if (num.toString() == "NaN") {
		num=1;
	}
	if(flag==1){
		num++;
	}else if(flag==-1){
		if(num>1){
			num--;
		}
	}
	$(obj).parent().find(":text").val(num);
}